package com.sphd.restaurantsystem.adapters;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.flyco.labelview.LabelView;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.base.RestaurantsApplication;
import com.sphd.restaurantsystem.info.GreensInfo;
import com.sphd.restaurantsystem.info.SortInfo;
import com.sphd.restaurantsystem.interfaces.FoodListBuyNo;
import com.sphd.restaurantsystem.utils.Constant;
import com.wzh.frame.utils.BigDisMailUtils;
import com.wzh.frame.views.RoundImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2016/11/23.
 */

public class FoodListAdapter extends BaseAdapter {
    int layout;
    List<GreensInfo> greensInfos;
    List<SortInfo> sortInfos;
    FoodListBuyNo foodListBuyNo;

    public void setFoodListBuyNo(FoodListBuyNo foodListBuyNo) {
        this.foodListBuyNo = foodListBuyNo;
    }

    public FoodListAdapter(int item_food_list_order, List<GreensInfo> greensInfos) {
        this.layout = item_food_list_order;
        this.greensInfos = greensInfos;
    }


    @Override
    public int getCount() {
        return greensInfos.size();
    }

    @Override
    public Object getItem(int i) {
        return greensInfos.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        GreensInfo greensInfo=greensInfos.get(i);
        ViewHolder viewHolder=null;
        if (view == null) {
            view = View.inflate(viewGroup.getContext(), R.layout.item_food_list_order, null);
            viewHolder=new ViewHolder(view);
            view.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) view.getTag();
        }
        viewHolder.foodName.setText(greensInfo.getDishes_name());
        viewHolder.foodtogo.setText(greensInfo.getSort());
        viewHolder.foodPrice.setText(BigDisMailUtils.turnTrim(greensInfo.getDishes_price(), Constant.DECIMAL_POINT));
        viewHolder.foodNum.setText(greensInfo.getBuyNo()==0?"":greensInfo.getBuyNo()+"");
        setImage(viewHolder.foodImage,greensInfo.getDishes_pic());
        ViewHolder finalViewHolder = viewHolder;
        viewHolder.addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                greensInfo.setBuyNo(greensInfo.getBuyNo()+1);
                finalViewHolder.foodNum.setText(greensInfo.getBuyNo()+"");
                foodListBuyNo.itemPos(greensInfo);
                foodListBuyNo.itemAdd(greensInfo.getDishes_price());
            }
        });
        viewHolder.canncelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (greensInfo.getBuyNo()>=1){
                    greensInfo.setBuyNo(greensInfo.getBuyNo()-1);
                    finalViewHolder.foodNum.setText(greensInfo.getBuyNo()+"");
                    foodListBuyNo.itemPos(greensInfo);
                    foodListBuyNo.itemSub(greensInfo.getDishes_price());
                }
            }
        });
        return view;
    }

    public void setImage(ImageView image, String url) {
        ImageLoader imageLoader = RestaurantsApplication.getImageLoader();
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(image,
                R.mipmap.moren, R.mipmap.moren);
        imageLoader.get(RestaurantUrl.Httpimg + url, listener);
    }

    static class ViewHolder {
        @BindView(R.id.food_image)
        RoundImageView foodImage;
        @BindView(R.id.food_name)
        TextView foodName;
        @BindView(R.id.foodtogo)
        LabelView foodtogo;
        @BindView(R.id.food_price)
        TextView foodPrice;
        @BindView(R.id.canncel_button)
        ImageView canncelButton;
        @BindView(R.id.food_num)
        TextView foodNum;
        @BindView(R.id.add_button)
        ImageView addButton;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}
